package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.jackrain.nea.core.redis.RedisObjectSerializer;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewDLQueryCmd;
import com.jackrain.nea.fc.dl.mapper.ViewDLQueryMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

/**
 * @author wuwenxue
 * @date 2018/07/02
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class ViewDLQueryCmdImpl extends CommandAdapter implements ViewDLQueryCmd {

    private RedisObjectSerializer serializer = new RedisObjectSerializer();

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        ViewDLQueryMapper viewDLQueryMapper = ApplicationContextHandle.getBean(ViewDLQueryMapper.class);

        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(
                map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewDLQueryCmdImpl--param-->{}",param);

        try {
            String sql = param.getString("dlsql");
            List<HashMap> dlLsit = viewDLQueryMapper.selsql(sql);
//            log.info("sql-->"+sql);
            byte[] dlbyte = serializer.serialize(dlLsit);
            vh.put("dlLsit",new String(dlbyte, "ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return vh;
    }
}
